package cn.itcast.springboot.listener;

import cn.itcast.springboot.config.RabbitConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

@Component
public class AckQueueListener implements ChannelAwareMessageListener {
    @RabbitListener(queues = RabbitConfig.QUEUE1)
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println("接收消息，处理中~~~");
        try {
            Thread.sleep(1000);
            System.out.println(new String(message.getBody()) + "~~~AckQueueListener");
//            int i = 1 / 0;
            //返回处理成功消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            //重回队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
